package org.linlinjava.litemall.wx.web;

import org.linlinjava.litemall.core.qcode.QCodeService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.service.LitemallSystemConfigService;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.service.WxReadingDiaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/wx/readingDiary")
public class WxReadingDiaryController {
    @Autowired
    private WxReadingDiaryService wxReadingDiaryService;
    @Autowired
    private QCodeService qCodeService;
    @Autowired
    private LitemallSystemConfigService systemConfigService;

    /**
     * 查询最近读的3本书和一共读过多少本
     * @param userId
     * @return
     */
    @GetMapping("/record")
    public Object queryRecentRead(@LoginUser Integer userId) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        Map<String, Object> data = new HashMap<>(4);
        Long countRead = wxReadingDiaryService.countRead(userId);
        Object recentReading = wxReadingDiaryService.recentReading(userId);
        String qCode = qCodeService.createShareReadingReportImage(userId);

        data.put("qCode",qCode);
        data.put("countRead",countRead);
        data.put("recentReading",recentReading);
        return ResponseUtil.ok(data);
    }

    /**
     * 查询阅读日记背景图片
     * @return
     */
    @GetMapping("/background")
    public Map<String, String> queryReadingBackground() {
        return systemConfigService.getReadingBackground();
    }
}
